package com.yc.http.tomcat.impl;

import com.yc.http.tomcat.*;

import java.io.IOException;

public class AddCookieServlet extends HttpServletImpl {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String name = request.getParameter("name");
        String value = request.getParameter("value");
        String maxAge = request.getParameter("maxAge");
        Integer iMaxAge = null;
        try{
            iMaxAge = Integer.parseInt(maxAge);
        }catch (NumberFormatException e){
            System.out.println("maxAge 数字格式错误: " + maxAge);
        }
        Cookie cookie = new CookieImpl(name,value);
        // 设置有效期
        /**
         * 设置有效期
         * 1. cookie保存到硬盘
         * 2. 有效期==0 => 删除cookie
         */
        cookie.setMaxAge(iMaxAge);
        response.addCookie(cookie);
        response.sendRedirect("showCookie.s");
    }
}
